package com.mdd.front.service.impl.yangyangLeadingEnterprise;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.mdd.common.core.PageResult;
import com.mdd.common.entity.yangyangLeadingEnterprise.TAppUserPublicWelfare;
import com.mdd.common.entity.yangyangLeadingEnterprise.common.JsonReturn;
import com.mdd.common.mapper.yangyangLeadingEnterprise.TAppUserPublicWelfareMapper;
import com.mdd.common.util.TimeUtils;
import com.mdd.front.service.yangyangLeadingEnterprise.ITAppUserPublicWelfareService;
import com.mdd.front.validate.common.PageValidate;
import com.mdd.front.validate.yangyangLeadingEnterprise.TAppUserPublicWelfareCreateValidate;
import com.mdd.front.validate.yangyangLeadingEnterprise.TAppUserPublicWelfareSearchValidate;
import com.mdd.front.validate.yangyangLeadingEnterprise.TAppUserPublicWelfareUpdateValidate;
import com.mdd.front.vo.yangyangLeadingEnterprise.TAppUserPublicWelfareDetailVo;
import com.mdd.front.vo.yangyangLeadingEnterprise.TAppUserPublicWelfareListedVo;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.util.Assert;

import javax.annotation.Resource;
import java.util.Date;
import java.util.LinkedList;
import java.util.List;

/**
 * 用户捐赠公益项目实现类
 * @author LikeAdmin
 */
@Service
public class TAppUserPublicWelfareServiceImpl implements ITAppUserPublicWelfareService {
        
    @Resource
    TAppUserPublicWelfareMapper tAppUserPublicWelfareMapper;

    /**
     * 用户捐赠公益项目列表
     *
     * @author LikeAdmin
     * @param pageValidate 分页参数
     * @param searchValidate 搜索参数
     * @return PageResult<TAppUserPublicWelfareListedVo>
     */
    @Override
    public PageResult<TAppUserPublicWelfareListedVo> list(PageValidate pageValidate, TAppUserPublicWelfareSearchValidate searchValidate) {
        Integer page  = pageValidate.getPage_no();
        Integer limit = pageValidate.getPage_size();

        QueryWrapper<TAppUserPublicWelfare> queryWrapper = new QueryWrapper<>();
        queryWrapper.orderByDesc("id");

        tAppUserPublicWelfareMapper.setSearch(queryWrapper, searchValidate, new String[]{
            "=:userId@user_id:int",
            "=:publicWelfareId@public_welfare_id:int",
            "=:enterpriseId@enterprise_id:int",
        });

        IPage<TAppUserPublicWelfare> iPage = tAppUserPublicWelfareMapper.selectPage(new Page<>(page, limit), queryWrapper);

        List<TAppUserPublicWelfareListedVo> list = new LinkedList<>();
        for(TAppUserPublicWelfare item : iPage.getRecords()) {
            TAppUserPublicWelfareListedVo vo = new TAppUserPublicWelfareListedVo();
            BeanUtils.copyProperties(item, vo);
            vo.setCreateTime(TimeUtils.timestampToDate(item.getCreateTime().getTime()));
            vo.setUpdateTime(TimeUtils.timestampToDate(item.getUpdateTime().getTime()));
            list.add(vo);
        }

        return PageResult.iPageHandle(iPage.getTotal(), iPage.getCurrent(), iPage.getSize(), list);
    }

    /**
     * 用户捐赠公益项目详情
     *
     * @author LikeAdmin
     * @param id 主键参数
     * @return TAppUserPublicWelfare
     */
    @Override
    public TAppUserPublicWelfareDetailVo detail(Integer id) {
        TAppUserPublicWelfare model = tAppUserPublicWelfareMapper.selectOne(
                new QueryWrapper<TAppUserPublicWelfare>()
                    .eq("id", id)
                    .last("limit 1"));

        Assert.notNull(model, "数据不存在");

        TAppUserPublicWelfareDetailVo vo = new TAppUserPublicWelfareDetailVo();
        BeanUtils.copyProperties(model, vo);
        return vo;
    }

    /**
     * 用户捐赠公益项目新增
     *
     * @author LikeAdmin
     * @param createValidate 参数
     */
    @Override
    public void add(TAppUserPublicWelfareCreateValidate createValidate) {
        TAppUserPublicWelfare model = new TAppUserPublicWelfare();
        model.setUserId(createValidate.getUserId());
        model.setPublicWelfareId(createValidate.getPublicWelfareId());
        model.setEnterpriseId(createValidate.getEnterpriseId());
        model.setCreateTime(new Date());
        model.setUpdateTime(new Date());
        tAppUserPublicWelfareMapper.insert(model);
    }

    /**
     * 用户捐赠公益项目编辑
     *
     * @author LikeAdmin
     * @param updateValidate 参数
     */
    @Override
    public void edit(TAppUserPublicWelfareUpdateValidate updateValidate) {
        TAppUserPublicWelfare model = tAppUserPublicWelfareMapper.selectOne(
                new QueryWrapper<TAppUserPublicWelfare>()
                    .eq("id",  updateValidate.getId())
                    .last("limit 1"));

        Assert.notNull(model, "数据不存在!");

        model.setId(updateValidate.getId());
        model.setUserId(updateValidate.getUserId());
        model.setPublicWelfareId(updateValidate.getPublicWelfareId());
        model.setEnterpriseId(updateValidate.getEnterpriseId());
        model.setUpdateTime(new Date());
        tAppUserPublicWelfareMapper.updateById(model);
    }

    /**
     * 用户捐赠公益项目删除
     *
     * @author LikeAdmin
     * @param id 主键ID
     */
    @Override
    public void del(Integer id) {
        TAppUserPublicWelfare model = tAppUserPublicWelfareMapper.selectOne(
                new QueryWrapper<TAppUserPublicWelfare>()
                    .eq("id", id)
                    .last("limit 1"));

        Assert.notNull(model, "数据不存在!");

        tAppUserPublicWelfareMapper.delete(new QueryWrapper<TAppUserPublicWelfare>().eq("id", id));
    }

    /**
     * 用户捐赠公益项目新增
     * @param createValidate
     * @return
     */
    @Override
    public String addUserPublicWelfare(TAppUserPublicWelfareCreateValidate createValidate) {
        TAppUserPublicWelfare model = new TAppUserPublicWelfare();
        model.setUserId(createValidate.getUserId());
        model.setPublicWelfareId(createValidate.getPublicWelfareId());
        model.setEnterpriseId(createValidate.getEnterpriseId());
        model.setCreateTime(new Date());
        model.setUpdateTime(new Date());
        tAppUserPublicWelfareMapper.insert(model);
        return JsonReturn.success().toJsonStr();
    }

}
